package org.anddev.andengine.input.touch.detector;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.input.touch.TouchEvent;

import android.os.SystemClock;
import android.speech.tts.TextToSpeech.Engine;
import android.view.MotionEvent;

/**
 * Note: Needs to be registered as an {@link IUpdateHandler} to the {@link Engine} or {@link Scene} to work properly.
 * 
 * @author Nicolas Gramlich
 * @since 20:49:25 - 23.08.2010
 */
public class HoldDetector extends BaseDetector implements IUpdateHandler {
	// ===========================================================
	// Constants
	// ===========================================================

	private static final float TRIGGER_HOLD_MINIMUM_SECONDS_DEFAULT = 0.08f;
	private static final float TIME_BETWEEN_UPDATES_DEFAULT = 0.1f;

	// ===========================================================
	// Fields
	// ===========================================================

	private float mTriggerHoldMaximumDistance;
	private final IHoldDetectorListener mHoldDetectorListener;

	private long mDownTimeMilliseconds = Long.MIN_VALUE;

	private float mHoldX;
	private float mHoldY;

	private boolean mTriggerOnHold = false;
	private boolean mHolding = false;

	private final TimerHandler mTimerHandler;
	private TimerHandler mTimerHandlerHoldTrigger;

	// ===========================================================
	// Constructors
	// ===========================================================

	public HoldDetector(final IHoldDetectorListener pClickDetectorListener) {
		this(TRIGGER_HOLD_MINIMUM_SECONDS_DEFAULT, TIME_BETWEEN_UPDATES_DEFAULT, pClickDetectorListener);
	}

	public HoldDetector(final float pTriggerHoldMinimumSeconds, final float pTimeBetweenUpdates, final IHoldDetectorListener pClickDetectorListener) {
		this.mHoldDetectorListener = pClickDetectorListener;

		this.mTimerHandler = new TimerHandler(pTimeBetweenUpdates, true, new ITimerCallback() {
			@Override
			public void onTimePassed(final TimerHandler pTimerHandler) {
				HoldDetector.this.fireListener();
			}
		});
		

		this.mTimerHandlerHoldTrigger = new TimerHandler(pTriggerHoldMinimumSeconds, new ITimerCallback() {
			private boolean mFirstTime = true;
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				if(this.mFirstTime){
					this.mFirstTime = false;
				} else {
					HoldDetector.this.startHolding();
				}
			}
		});
	}

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	public float getTriggerHoldMinimumSeconds() {
		return this.mTimerHandlerHoldTrigger.getTimerSeconds();
	}

	public void setTriggerHoldMinimumSeconds(final float pTriggerHoldMinimumSeconds) {
		this.mTimerHandlerHoldTrigger.setTimerSeconds(pTriggerHoldMinimumSeconds);
	}

	public float getTriggerHoldMaximumDistance() {
		return this.mTriggerHoldMaximumDistance;
	}

	public void setTriggerHoldMaximumDistance(final float pTriggerHoldMaximumDistance) {
		this.mTriggerHoldMaximumDistance = pTriggerHoldMaximumDistance;
	}

	public boolean isHolding() {
		return this.mTriggerOnHold;
	}

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public void onUpdate(final float pSecondsElapsed) {
		if(isEnabled()){
			this.mTimerHandler.onUpdate(pSecondsElapsed);
			this.mTimerHandlerHoldTrigger.onUpdate(pSecondsElapsed);
		}else {
			this.stopHolding();
		}
	}

	@Override
	public void reset() {
		this.mTimerHandler.reset();
	}

	@Override
	public boolean onManagedTouchEvent(final TouchEvent pSceneTouchEvent) {
		final MotionEvent motionEvent = pSceneTouchEvent.getMotionEvent();

		this.mHoldX = pSceneTouchEvent.getX();
		this.mHoldY = pSceneTouchEvent.getY();

		switch(pSceneTouchEvent.getAction()) {
			case MotionEvent.ACTION_DOWN:
				this.mDownTimeMilliseconds = motionEvent.getDownTime();
				this.mTimerHandlerHoldTrigger.reset();
				this.mHolding = true;
				return true;
			case MotionEvent.ACTION_MOVE:
				return true;
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_CANCEL:
				this.stopHolding();
				return true;
			default:
				return false;
		}
	}

	// ===========================================================
	// Methods
	// ===========================================================

	protected void fireListener() {
		if(this.mTriggerOnHold) {
			this.mHoldDetectorListener.onHold(this, SystemClock.uptimeMillis() - this.mDownTimeMilliseconds, this.mHoldX, this.mHoldY);
		}
	}
	
	protected void startHolding(){
		if(!this.mTriggerOnHold && this.mHolding) {
			this.mHoldDetectorListener.onHoldStarted(this, SystemClock.uptimeMillis() - this.mDownTimeMilliseconds, this.mHoldX, this.mHoldY);
			this.mTriggerOnHold = true;
		}
	}
	
	protected void stopHolding(){
		this.mHolding = false;
		if(this.mTriggerOnHold) {
			this.mHoldDetectorListener.onHoldFinished(this, SystemClock.uptimeMillis() - this.mDownTimeMilliseconds, this.mHoldX, this.mHoldY);
			this.mTriggerOnHold = false;
		}
	}

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================

	public static interface IHoldDetectorListener {
		// ===========================================================
		// Constants
		// ===========================================================

		// ===========================================================
		// Methods
		// ===========================================================
		
		public void onHoldStarted(final HoldDetector pHoldDetector, final long pHoldTimeMilliseconds, final float pHoldX, final float pHoldY);
		public void onHold(final HoldDetector pHoldDetector, final long pHoldTimeMilliseconds, final float pHoldX, final float pHoldY);
		public void onHoldFinished(final HoldDetector pHoldDetector, final long pHoldTimeMilliseconds, final float pHoldX, final float pHoldY);
	}
}
